/*
 * Copyright 2010 Yaroslav Klymko aka t3hnar
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ua.t3hnar.io.serializer.handlers;

import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.sql.rowset.serial.SerialBlob;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.SQLException;

/**
 * @author Yaroslav Klymko aka t3hnar
 */
public class BlobHandler extends FieldHandler<Blob> {
// ------------------------------ FIELDS ------------------------------

    private static final String BLOB = "blob";
    public static final Class<Blob> BLOB_CLASS = Blob.class;

    private static final Log LOG = LogFactory.getLog(BlobHandler.class);

// --------------------------- CONSTRUCTORS ---------------------------

    protected BlobHandler() {
        super(BLOB, BLOB_CLASS);

        LOG.debug("Initializing BlobHandler");
    }

// -------------------------- OTHER METHODS --------------------------

    @Override
    public Blob toInstance(final InputStream inputStream) throws ToInstanceException {
        LOG.debug("Retrieving instance; inputStream: " + inputStream);
        if (inputStream == null) {
            throw new IllegalArgumentException("InputStream is null");
        }

        final byte[] bytes;
        try {
            bytes = IOUtils.toByteArray(inputStream);
        } catch (IOException e) {
            final String message = "Failed to read stream: " + e;
            LOG.debug(message);
            throw new ToInstanceException(message, e);
        }

        try {
            return new SerialBlob(bytes);
        } catch (SQLException e) {
            final String message = "Failed to create blob: " + e;
            LOG.debug(message);
            throw new ToInstanceException(message, e);
        }
    }

    @Override
    public InputStream toStream(final Object instance) throws ToStreamException {
        LOG.debug("Retrieving stream; instance: " + instance);
        if (instance == null) {
            throw new IllegalArgumentException("Instance is null");
        }

        if (instance instanceof Blob) {
            final Blob blob = (Blob) instance;
            try {
                return blob.getBinaryStream();
            } catch (SQLException e) {
                final String message = "Failed to retrieve stream: " + e;
                LOG.debug(message);
                throw new ToStreamException(message, e);
            }
        } else {
            throw new IllegalArgumentException("Instance is not an instance of Blob");
        }
    }
}
